<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="评论内容的mongo_id" prop="contentId">
      <el-input v-model="dataForm.contentId" placeholder="评论内容的mongo_id"></el-input>
    </el-form-item>
    <el-form-item label="评论用户id" prop="fromId">
      <el-input v-model="dataForm.fromId" placeholder="评论用户id"></el-input>
    </el-form-item>
    <el-form-item label="评论用户昵称" prop="fromName">
      <el-input v-model="dataForm.fromName" placeholder="评论用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="评论人头像路径" prop="fromAvatar">
      <el-input v-model="dataForm.fromAvatar" placeholder="评论人头像路径"></el-input>
    </el-form-item>
    <el-form-item label="评论目标用户" prop="toId">
      <el-input v-model="dataForm.toId" placeholder="评论目标用户"></el-input>
    </el-form-item>
    <el-form-item label="评论用户昵称" prop="toName">
      <el-input v-model="dataForm.toName" placeholder="评论用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="景点/文章/评论id" prop="topicId">
      <el-input v-model="dataForm.topicId" placeholder="景点/文章/评论id"></el-input>
    </el-form-item>
    <el-form-item label="评论类型：景点1/文章2/评论3" prop="topicType">
      <el-input v-model="dataForm.topicType" placeholder="评论类型：景点1/文章2/评论3"></el-input>
    </el-form-item>
    <el-form-item label="删除标志（0代表存在 1代表删除）" prop="deleteStatus">
      <el-input v-model="dataForm.deleteStatus" placeholder="删除标志（0代表存在 1代表删除）"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          contentId: '',
          fromId: '',
          fromName: '',
          fromAvatar: '',
          toId: '',
          toName: '',
          topicId: '',
          topicType: '',
          deleteStatus: '',
          createTime: ''
        },
        dataRule: {
          contentId: [
            { required: true, message: '评论内容的mongo_id不能为空', trigger: 'blur' }
          ],
          fromId: [
            { required: true, message: '评论用户id不能为空', trigger: 'blur' }
          ],
          fromName: [
            { required: true, message: '评论用户昵称不能为空', trigger: 'blur' }
          ],
          fromAvatar: [
            { required: true, message: '评论人头像路径不能为空', trigger: 'blur' }
          ],
          toId: [
            { required: true, message: '评论目标用户不能为空', trigger: 'blur' }
          ],
          toName: [
            { required: true, message: '评论用户昵称不能为空', trigger: 'blur' }
          ],
          topicId: [
            { required: true, message: '景点/文章/评论id不能为空', trigger: 'blur' }
          ],
          topicType: [
            { required: true, message: '评论类型：景点1/文章2/评论3不能为空', trigger: 'blur' }
          ],
          deleteStatus: [
            { required: true, message: '删除标志（0代表存在 1代表删除）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/member/comment/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.contentId = data.comment.contentId
                this.dataForm.fromId = data.comment.fromId
                this.dataForm.fromName = data.comment.fromName
                this.dataForm.fromAvatar = data.comment.fromAvatar
                this.dataForm.toId = data.comment.toId
                this.dataForm.toName = data.comment.toName
                this.dataForm.topicId = data.comment.topicId
                this.dataForm.topicType = data.comment.topicType
                this.dataForm.deleteStatus = data.comment.deleteStatus
                this.dataForm.createTime = data.comment.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/member/comment/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'contentId': this.dataForm.contentId,
                'fromId': this.dataForm.fromId,
                'fromName': this.dataForm.fromName,
                'fromAvatar': this.dataForm.fromAvatar,
                'toId': this.dataForm.toId,
                'toName': this.dataForm.toName,
                'topicId': this.dataForm.topicId,
                'topicType': this.dataForm.topicType,
                'deleteStatus': this.dataForm.deleteStatus,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
